#ifndef __GL_EXT_UTILS__
#define __GL_EXT_UTILS__

#include <string>
#include <stdio.h>

#include "allocator.hpp"

namespace zen {

	namespace gl {

		namespace utils {

			/*template <typename ValueType>
			inline ValueType min(const ValueType a, const ValueType b) {
				return a > b ? b : a;
			}

			template <typename ValueType>
			inline ValueType max(const ValueType a, const ValueType b) {
				return a < b ? b : a;
			}*/

			inline std::string dirname(const std::string &value) {
				size_t pos = value.rfind('/');
				if (pos != std::string::npos)
					return value.substr(0, pos + 1);
				return "";
			}

			inline size_t get_file_contents(const std::string &fileName, char* &content) {
				FILE* file = NULL;
				errno_t error = fopen_s(&file, fileName.c_str(), "rb");
				if (error != 0 || file == NULL) {
					content = nullptr;
					return 0;
				}
				fseek(file, 0, SEEK_END);
				size_t size = ftell(file);
				content = (char*)zglAllocate(size + 1);
				if (content == NULL) {
					fclose(file);
					return 0;
				}
				fseek(file, 0, SEEK_SET);
				size_t rsize = fread(content, 1, size, file);
				fclose(file);
				if (rsize != size) {
					zglDeallocate(content);
					content = nullptr;
					return 0;
				}
				content[size] = 0;
				return size + 1;
			}

		} // namespace detail

	} // namespace gl

} // namespace zen

#endif // __GL_EXT_UTILS__